﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Threading;
using System.Reflection; // 引用这个才能使用Missing字段
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;

using ZedGraph;
using System.IO;
using System.Diagnostics;

namespace weiqijiance
{
    public partial class TESTSU : Form
    {
        public string sshebei = "悬架";
        public mainform m_mainform;
        public bool cbjd;
        //public string sdetid;
        /// <summary>
        /// 结果变量
        /// </summary>
        public float flunzhongzuo;
        public float flunzhongyou;
        public float flunzhongzuo1;
        public float flunzhongyou1;

        public int xssj;
        public Thread t1;
        public Thread t2;
        public string xianshineirong;
        public string businessId;

        public int iSUHW;//红外

        public int iSUtdz;//悬架信号接在那个通道
        public int iSUtdy;//悬架信号接在那个通道

        public int iSUDJz;//电机左
        public int iSUDJy;//电机右

        float[] m_GCSJZ = new float[9000];//过程数据左
        float[] m_GCSJY = new float[9000];//过程数据右



        //红外取反
        public bool[] bhongwaiqf = new bool[16] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };//红外是否取反
        //红外定位时间
        public int[] ihongwaidwsj = new int[16] { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };//红外定位时间单位毫秒


        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;


        public 控制流程.flowcrtl flowbass = new 控制流程.flowcrtl();
        public TESTSU(int Width, int Height)
        {
            InitializeComponent();
            panel_Width = Width;
            panel_Height = Height;
            gettongdao();
            //this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            x = this.Width;
            y = this.Height;
            setTag(this);
        }
        /// <summary>
        /// 变量初始化
        /// </summary>
        public void bianliangchushihua()
        {
            flunzhongzuo = 0;
            flunzhongyou = 0;


            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\车辆信息.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

        }
        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            if (newy <= 0)
            {
                newy = 1;
            }
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion

        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
        //ini文件


        public void init()
        {
            iniclass objini = new iniclass();
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\安综检仪表配置.ini";
            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);
            int.TryParse(objini.ReadValue("悬架", "信息显示时间"), out xssj);
            if (xssj < 100)
            {
                xssj = 1000;
            }


        }

        private delegate void FlushClient();//代理
        private void xianshi()
        {
            if (this.xinxixianshi.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshi);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.xinxixianshi.Text = string.Format(@"{0}", xianshineirong);
            }
        }

        private void xianshizuo()
        {
            if (this.gauge_weight_left.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshizuo);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.gauge_weight_left.Visible = true;
                this.digitalGauge1.DigitCount = flunzhongzuo.ToString("0").Length < 3 ? 3 : flunzhongzuo.ToString("0.0").Length;
                this.digitalGauge1.Text = flunzhongzuo.ToString("0");
                //this.lunzhongzou.Text = string.Format(@"{0}", flunzhongzuo.ToString("0.0"));
            }
        }
        private void xianshiyou()
        {
            if (this.gauge_weight_right.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiyou);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.gauge_weight_right.Visible = true;
                this.digitalGauge2.DigitCount = flunzhongyou.ToString("0").Length < 3 ? 3 : flunzhongyou.ToString("0.0").Length;
                this.digitalGauge2.Text = flunzhongyou.ToString("0");
                //this.lunzhongyou.Text = string.Format(@"{0}", flunzhongyou.ToString("0.0"));
            }
        }
        private void xianshizuo1()
        {
            if (this.gauge_zhidonglizuo.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshizuo1);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.gauge_zhidonglizuo.Visible = true;
                this.digitalGauge4.DigitCount = flunzhongzuo1.ToString("0.00").Length < 3 ? 3 : flunzhongzuo1.ToString("0.00").Length;
                this.digitalGauge4.Text = flunzhongzuo1.ToString("0.00");
               // this.zhidonglizuo.Text = string.Format(@"{0}", flunzhongzuo1.ToString("0.00"));
            }
        }
        private void xianshiyou1()
        {
            if (this.gauge_zhidongliyou.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiyou1);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.gauge_zhidongliyou.Visible = true;
                this.digitalGauge3.DigitCount = flunzhongyou1.ToString("0.00").Length < 3 ? 3 : flunzhongyou1.ToString("0.00").Length;
                this.digitalGauge3.Text = flunzhongyou1.ToString("0.00");
               // this.zhidongliyou.Text = string.Format(@"{0}", flunzhongyou1.ToString("0.00"));
            }
        }


        protected void thisclose()
        {

            this.Invoke(new Action(this.Close));  //使用委托关闭该窗体
        }
        public void start()//(string sbusinessId, string sjylsh)
        {
            // jylsh = sjylsh;
            //businessId = sbusinessId;
            bianliangchushihua();
            t1 = new Thread(new ThreadStart(Thread1));
            t2 = new Thread(new ThreadStart(Thread2));
            t2.Priority = ThreadPriority.Lowest;
            t2.Start();
            //if (m_mainform.szdksjc.Contains("是"))
            {
                //t1 = new Thread(new ThreadStart(Thread1));
                t1.Priority = ThreadPriority.BelowNormal;
                t1.Start();
            }
        }
        public void gettongdao()
        {

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            for (int i = 0; i < 10; i++)
            {
                string sgetname = ReadValue("通道设置", string.Format(@"模拟信号{0}", i + 1));
                if (sgetname.Contains("悬架左"))
                {
                    iSUtdz = i;
                }

                if (sgetname.Contains("悬架右"))
                {
                    iSUtdy = i;
                }


            }



            for (int i = 0; i < 16; i++)
            {
                string shongwai = ReadValue("通道设置", string.Format(@"红外{0}", i + 1));
                if (shongwai.Contains("悬架到位"))
                {
                    iSUHW = i;
                }


                shongwai = ReadValue("通道设置", string.Format(@"红外定位时间{0}", i + 1));
                int.TryParse(shongwai, out ihongwaidwsj[i]);


                shongwai = ReadValue("通道设置", string.Format(@"红外是否取反{0}", i + 1));
                if (shongwai.Contains("是"))
                {
                    bhongwaiqf[i] = true;
                }
                else
                {
                    bhongwaiqf[i] = false;
                }
            }
            ///控制
            for (int i = 0; i < 8; i++)
            {

                string shongwai = ReadValue("通道设置", string.Format(@"控制{0}", i + 1));
                if (shongwai.Contains("悬架电机左"))
                {
                    iSUDJz = i;
                }
                if (shongwai.Contains("悬架电机右"))
                {
                    iSUDJy = i;
                }
            }
        }

        public void StratTest()
        {
            //m_mainform.DAC.tiaoling(1);//调零
            //m_mainform.getstand(businessId);//读取限值标准
            //ShowControls(true);
            init();
            if (!flowbass.bJCXJ)
            {
                xianshineirong = string.Format(@"悬架免检");
                xianshi();
                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                //m_mainform.WaitEventMJ(iSUHW, bhongwaiqf[iSUHW], ihongwaidwsj[iSUHW]);
                // 红外定位
               // WaitEvent(iSUHW, bhongwaiqf[iSUHW], ihongwaidwsj[iSUHW]);
                return;
            }
            flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "悬架检测开始", flowbass.sDetectID);
            for (int i = 0; i < 2; i++)
            {
                DetectVax(i);
                if (flowbass.IsCancle())
                {
                    flowbass.LedWrite("检测取消", "", 1);
                    break;
                }
            }
            Thread.Sleep(1000);

            xianshineirong = string.Format(@"测试完成");
            xianshi();
            //m_mainform.flowwq.LedWrite(xianshineirong, "", 1);
            flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
            flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "悬架检测结束", flowbass.sDetectID);


            //检测结束

            //保存数据
           



        }

        /// <summary>
        /// 红外信号判断是否到位 路数 是否取反 保持稳定时间
        /// </summary>
        /// <param name="sName"></param>
        public void WaitEvent(int lushu, bool bqufan, int stime)
        {
            DateTime dstart = DateTime.Now;
            bool bzt = false;
            while (true)
            {
                if (flowbass.IsCancle())
                {
                    flowbass.LedWrite("检测取消", "", 1);
                    return;
                }
                bool bzhuangtai = m_mainform.DAC.bDIData[lushu];
                if (bqufan)
                {
                    bzhuangtai = !bzhuangtai;
                }
                if (bzhuangtai)
                {
                    if (!bzt)
                    {
                        //flowbass.LedWrite(flowbass.sPlate, "请驶上制动台", 1);
                        dstart = DateTime.Now;
                    }
                    else
                    {
                        if (DateTime.Now > dstart.AddMilliseconds(stime))
                        {
                            break;
                        }
                        flowbass.LedWrite(flowbass.sPlate, "停车", 1);
                    }
                }
                else
                {
                    dstart = DateTime.Now;
                    bzt = true;
                    flowbass.LedWrite(flowbass.sPlate, "未到位", 1);
                }
                Thread.Sleep(100);
            }
        }
        public void DetectVax(int nVaxID)
        {
            string DET_JTLZZ = string.Format("DET_JTLZZ{0}", nVaxID + 1);
            string DET_JTLZY = string.Format("DET_JTLZY{0}", nVaxID + 1);
            string DET_DTLZZ = string.Format("DET_DTLZZ{0}", nVaxID + 1);
            string DET_DTLZY = string.Format("DET_DTLZY{0}", nVaxID + 1);
            string DET_XJC = string.Format("DET_XJC{0}", nVaxID + 1);
            string DET_XJQXZ = string.Format("DET_XJQXZ{0}", nVaxID + 1);
            string DET_XJQXY = string.Format("DET_XJQXY{0}", nVaxID + 1);
            string DET_XJXSLZ = string.Format("DET_XJXSLZ{0}", nVaxID + 1);
            string DET_XJXSLY = string.Format("DET_XJXSLY{0}", nVaxID + 1);
            string DET_XJJL = string.Format("DET_XJJL{0}", nVaxID + 1);

            List<int> listxjL = new List<int>();
            List<int> listxjR = new List<int>();
            List<int> listlzL = new List<int>();
            List<int> listlzR = new List<int>();
            xianshineirong = string.Format(@"第{0}轴驶上悬架台", nVaxID + 1);
            xianshi();
            //m_mainform.flowwq.LedWrite(xianshineirong, "", 1);
            flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);

            // 红外定位
            WaitEvent(iSUHW, bhongwaiqf[iSUHW], ihongwaidwsj[iSUHW]);
            //Thread.Sleep(3000);
            if (flowbass.IsCancle())
            {
                flowbass.LedWrite("检测取消", "", 1);
                return;
            }

            //

            //测量数据
            DateTime dstart = DateTime.Now;
            //先测量静态轮重
            DateTime dstartCYZ = DateTime.Now;
            
            float flunzhong1 = 0;
            float flunzhong2 = 0;
            int iminL = 9999, iminR = 9999;

            for (int i = 0; i < 2; i++)//测量动态
            {
                if (i == 0)
                {

                    xianshineirong = string.Format(@"启动左电机");
                    xianshi();
                    m_mainform.DAC.LiftOne(true, (short)(iSUDJz));
                    flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                    Thread.Sleep(3000);

                }
                else
                {
                    xianshineirong = string.Format(@"启动右电机");
                    xianshi();
                    flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                    m_mainform.DAC.LiftOne(true, (short)(iSUDJy));
                    Thread.Sleep(3000);
                }
                if (i == 0)
                {
                    m_mainform.DAC.LiftOne(false, (short)(iSUDJz));
                }
                else
                {
                    m_mainform.DAC.LiftOne(false, (short)(iSUDJy));
                }
                flowbass.LedWrite(flowbass.sPlate, "停电机开始检测", 1);
                //Thread.Sleep(3000);
                int icount = 0;
                dstart = DateTime.Now;
                for (; ; )
                {
                    if (flowbass.IsCancle())
                    {
                        flowbass.LedWrite("检测取消", "", 1);
                        return;
                    }

                    //保证时间
                    if (m_mainform.DateDiff(DateTime.Now, dstart) > 5000)
                    {
                        break;
                    }

                    if (i == 0)
                    {

                        m_GCSJZ[icount] = m_mainform.DAC.getjsz("悬架左", m_mainform.DAC.fAIDataSU[iSUtdz]) - m_mainform.DAC.getjsz("悬架左", m_mainform.DAC.fAIDataZero[iSUtdz]);

                        listxjL.Add((int)m_GCSJZ[icount]);
                        if (iminL > (int)m_GCSJZ[icount])
                        {
                            iminL = (int)m_GCSJZ[icount];
                        }

                    }
                    else
                    {
                        m_GCSJY[icount] = m_mainform.DAC.getjsz("悬架右", m_mainform.DAC.fAIDataSU[iSUtdy]) - m_mainform.DAC.getjsz("悬架右", m_mainform.DAC.fAIDataZero[iSUtdy]);

                        listxjR.Add((int)m_GCSJY[icount]);
                        if (iminR > (int)m_GCSJY[icount])
                        {
                            iminR = (int)m_GCSJY[icount];
                        }
                    }
                    Thread.Sleep(10);
                }



                if (i == 0)
                {
                    flowbass.LedWrite(flowbass.sPlate, "悬架左检测完成", 1);
                    WriteGraphZUO(zg2, listxjL, 1, listxjL.Count, flunzhongzuo);
                }
                else
                {
                    flowbass.LedWrite(flowbass.sPlate, "悬架右检测完成", 1);
                    WriteGrapYOU(zedGraphControl1, listxjR, 1, listxjR.Count, flunzhongyou);
                }
                Thread.Sleep(1000);



            }//终止循环	

            Thread.Sleep(4000);

            xianshineirong = string.Format(@"测量静态轮重");
            xianshi();
            flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
            dstart = DateTime.Now;
            while (true)
            {
                if (flowbass.IsCancle())
                {
                    flowbass.LedWrite("检测取消", "", 1);
                    return;
                }
                if (m_mainform.DateDiff(DateTime.Now, dstart) > 5000)
                {
                    break;
                }

                dstartCYZ = DateTime.Now;

                flunzhong1 = 0;
                flunzhong2 = 0;
                flunzhong1 = m_mainform.DAC.getjsz("悬架左", m_mainform.DAC.fAIDataSU[iSUtdz]) - m_mainform.DAC.getjsz("悬架左", m_mainform.DAC.fAIDataZero[iSUtdz]);
                flunzhong2 = m_mainform.DAC.getjsz("悬架右", m_mainform.DAC.fAIDataSU[iSUtdy]) - m_mainform.DAC.getjsz("悬架右", m_mainform.DAC.fAIDataZero[iSUtdy]);

                listxjL.Add((int)flunzhong1);
                listxjR.Add((int)flunzhong2);
                WriteGraphZUO(zg2, listxjL, 1, listxjL.Count, flunzhongzuo);
                WriteGrapYOU(zedGraphControl1, listxjR, 1, listxjR.Count, flunzhongyou);
                flunzhongzuo = flunzhong1;// (float)(flunzhong1 / icyz);
                xianshizuo();
                flunzhongyou = flunzhong2;// (float)(flunzhong2 / icyz);
                xianshiyou();
                Thread.Sleep(20);
            }
            int ixjlzs = 0, ixjrzs = 0;
            for (int i = 0; i < listxjL.Count; i++)
            {
                ixjlzs += listxjL[i];
                ixjrzs += listxjR[i];
            }
            flunzhongzuo = (float)Math.Round((double)((float)ixjlzs / (float)listxjL.Count), 0);// (float)(flunzhong1 / icyz);
            xianshizuo();
            flunzhongyou = (float)Math.Round((double)((float)ixjrzs / (float)listxjR.Count), 0);// (float)(flunzhong2 / icyz);
            xianshiyou();
            //-------------------------------------------------------------------------
            //if (i == 0)
            {
                if (flunzhongzuo < 1)
                {
                    flunzhongzuo = 100;
                }
                else
                {
                    flunzhongzuo1 = float.Parse(((1 - (flunzhongzuo - iminL) / flunzhongzuo) * 100).ToString("0.00"));
                }
                xianshizuo1();


            }
            //else
            {

                if (flunzhongyou < 1)
                {
                    flunzhongyou = 100;
                }
                else
                {
                    flunzhongyou1 = float.Parse(((1 - (flunzhongyou - iminR) / flunzhongyou) * 100).ToString("0.00"));
                }
                xianshiyou1();


            }
            //--------------------------------------------------
            //float fxjc = Math.Round(Math.Abs( flunzhongzuo1 - flunzhongyou1), 1);
            float fxjc = Math.Abs(flunzhongzuo1 - flunzhongyou1);
            bool bGood = true;
            if (flunzhongzuo1 < 40 || flunzhongyou1 < 40 || fxjc>15)
            {
                bGood = false;
            }

            string sStr = string.Format("左:{0}%", flunzhongzuo1.ToString("0.00"));
            string sStr1 = string.Format("右:{0}%", flunzhongyou1.ToString("0.00"));
            string sStr2 = string.Format("检测{0}", bGood ? "合格" : "不合格");
            flowbass.LedWrite(sStr, sStr1, 1);
            Thread.Sleep(1000);
            flowbass.LedWrite(sStr2, "", 1);
            Thread.Sleep(1000);
            flowbass.AddData(flowbass.sDetectID, DET_XJJL, "Detects_SJC", bGood ? 1 : 2);//项目是否合格
            flowbass.AddData(flowbass.sDetectID, DET_XJXSLZ, "Detects_SJC", flunzhongzuo1.ToString("0.00"));//悬架左%
            flowbass.AddData(flowbass.sDetectID, DET_XJXSLY, "Detects_SJC", flunzhongyou1.ToString("0.00"));//悬架右%

            flowbass.AddData(flowbass.sDetectID, DET_XJC, "Detects_SJC", fxjc);//悬架差
            //flowbass.AddData(flowbass.sDetectID, "VAX_IPDTAD", "Detects_SJC", bGood);//	悬架结论

            flowbass.AddData(flowbass.sDetectID, DET_JTLZZ, "Detects_SJC", flunzhongzuo);//重量左
            flowbass.AddData(flowbass.sDetectID, DET_JTLZY, "Detects_SJC", flunzhongyou);//重量右

            flowbass.AddData(flowbass.sDetectID, DET_DTLZZ, "Detects_SJC", iminL);//动态重量左
            flowbass.AddData(flowbass.sDetectID, DET_DTLZY, "Detects_SJC", iminR);//动态重量右


            if (flunzhongzuo1 < 40 || flunzhongyou1 < 40 || fxjc > 15)
            {
                bGood = false;
            }
            if (!bGood)
            {
                flowbass.getbuhge(flowbass.sDetectID, "XJ");
            }
            else
            {

                flowbass.sethege(flowbass.sDetectID, "XJ");
            }
            string jielun = "";
            jielun = string.Format(@"{0},{1},{2},{3}", (nVaxID+1) + "轴悬架差(%)", fxjc.ToString("0.00"), "≤15", fxjc <= 15 ? "合格" : "不合格");

            flowbass.AddData(flowbass.sDetectID,string.Format("PD_XJC{0}",nVaxID+1), "PD_ZJ", jielun);
            jielun = string.Format(@"{0},{1},{2},{3}", (nVaxID + 1) + "轴悬架左(%)", flunzhongzuo1.ToString("0.00"), "≥40", flunzhongzuo1 >= 40 ? "合格" : "不合格");

            flowbass.AddData(flowbass.sDetectID, string.Format("PD_XJZ{0}", nVaxID + 1), "PD_ZJ", jielun);
            jielun = string.Format(@"{0},{1},{2},{3}", (nVaxID + 1) + "轴悬架右(%)", flunzhongyou1.ToString("0.00"), "≥40", flunzhongyou1 >= 40 ? "合格" : "不合格");

            flowbass.AddData(flowbass.sDetectID, string.Format("PD_XJY{0}", nVaxID + 1), "PD_ZJ", jielun);

            List<int> listxjL1 = new List<int>();
            List<int> listxjR1 = new List<int>();
            foreach (int ilist in listxjL)
            {
                listxjL1.Add(ilist);
            }
            foreach (int ilist in listxjR)
            {
                listxjR1.Add(ilist);
            }

            flowbass.editData(flowbass.sDetectID, DET_XJQXZ, "Detects_SJC", listxjL, listxjL1);
            flowbass.editData(flowbass.sDetectID, DET_XJQXY, "Detects_SJC", listxjR, listxjR1);
            //if(GetData('L'))//取曲线
            //{
            //    m_pDbCon->EditData(iDetectID,"VAX_VPRCUL","Vaxles",(unsigned char*)&m_CurData,sizeof(m_CurData.head)+m_CurData.head.nCount*sizeof(SU_CURVE_POINT::DATA),nVaxID+1);//写制动曲线
            //}
            //Sleep(100);
            //if(GetData('R'))//取曲线
            //{
            //    m_pDbCon->EditData(iDetectID,"VAX_VPRCUR","Vaxles",(unsigned char*)&m_CurData,sizeof(m_CurData.head)+m_CurData.head.nCount*sizeof(SU_CURVE_POINT::DATA),nVaxID+1);//写制动曲线
            //}

        }
        public void Thread1()
        {
            try
            {
              //  m_mainform.DAC.tiaoling(1);
                StratTest();
                flowbass.getProcessfinish(flowbass.sDetectID, "XJ");
                flowbass.LedWrite(flowbass.sgongwei, "", 1);
                //xianshineirong = flowbass.sgongwei;
                //xianshi();
                if (flowbass.LastProcessnewzj(int.Parse(flowbass.sDetectID)) && (flowbass.bzj || flowbass.bsj))
                {
                    xianshineirong = "检测线完成";
                    flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "检测线完成", flowbass.sDetectID);
                    flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                    flowbass.sethegezj(flowbass.sDetectID);
                    Thread.Sleep(2000);
                }
                flowbass.LedWrite(flowbass.sgongwei, "", 1);
                xianshineirong = flowbass.sgongwei;
                xianshi();
                end();
                //t1.Abort();
                //t1.DisableComObjectEagerCleanup();
            }
            catch (Exception ex)
            {
                flowbass.LedWrite(flowbass.sgongwei, "", 1);
                xianshineirong = flowbass.sgongwei;
                xianshi();
                //错误日记 记录到当天日期下目录中
                m_mainform.baocunriji("悬架", ex.Message.ToString());
            }
        }
        public void Thread2()
        {
           

        }
        public int panel_Width;
        public int panel_Height;   
        private void TESTSU_Resize(object sender, EventArgs e)
        {
           
        }

        private void TESTSU_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            start();//("1", "1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.LiftOne(true, (short)(iSUDJz));
         
        }

        private void button3_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.LiftOne(true, (short)(iSUDJy));
       
        }

        private void button4_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.LiftOne(false, (short)(iSUDJz));
            m_mainform.DAC.LiftOne(false, (short)(iSUDJy));
           

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (flowbass.izhuangtai == 2)
                {
                    m_mainform.objyibiaobass.select(flowbass.sshebei);
                    m_mainform.bpanelshow = true;
                    m_mainform.showpanel();
                    flowbass.izhuangtai = 1;
                    //Thread tt = new Thread(end);
                    //tt.Start();
                    bianliangchushihua();
                    flowbass.IsReadyForStart();
                    t2 = new Thread(new ThreadStart(Thread2));
                    t2.Priority = ThreadPriority.Lowest;
                    t2.Start();
                    t1 = new Thread(new ThreadStart(Thread1));
                    t1.Priority = ThreadPriority.BelowNormal;
                    t1.Start();
                    //this.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                m_mainform.baocunriji("悬架打开窗体失败", ex.Message.ToString());
                m_mainform.SaveErrorLog("悬架打开窗体", ex.Message.ToString());//problem
            }
        }
        public void timerstart()
        {
            timer1.Enabled = true;
        }
        public void end()
        {
           
            flowbass.izhuangtai = 0;
            flowbass.sendend("E", flowbass.sDetectID);
            //closeform();
        }
        private void closeform()
        {
            if (this.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(closeform);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.Dispose();
                this.Close();
            }
        }

        private void 仪表设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            工位配置.SUsetting obj = new 工位配置.SUsetting();
            obj.ShowDialog();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            try{
                m_mainform.sethongwai(lblhongwai1, m_mainform.WaitEventZT(iSUHW, bhongwaiqf[iSUHW], ihongwaidwsj[iSUHW]));
                m_mainform.sethongwai(toolStripLabel2, m_mainform.WaitEventZT(iSUHW, bhongwaiqf[iSUHW], ihongwaidwsj[iSUHW]));
            }
            catch 
            { }

            try
            {
                if (cbjd)
                {
                    Thread.Sleep(10);
                    float flunzhong1 = 0;
                    float flunzhong2 = 0;
                    flunzhong1 = m_mainform.DAC.getjsz("悬架左", m_mainform.DAC.fAIData[iSUtdz]) - m_mainform.DAC.getjsz("悬架左", m_mainform.DAC.fAIDataZero[iSUtdz]);
                    flunzhong2 = m_mainform.DAC.getjsz("悬架右", m_mainform.DAC.fAIData[iSUtdy]) - m_mainform.DAC.getjsz("悬架右", m_mainform.DAC.fAIDataZero[iSUtdy]);

                    flunzhongzuo = flunzhong1;
                    xianshizuo();
                    flunzhongyou = flunzhong2;
                    xianshiyou();
                    flowbass.LedWrite("悬架", string.Format("左:{0} 右:{1}", flunzhongzuo.ToString("0.00"), flunzhongyou.ToString("0.00")), 1);
                }
            }
            catch
            { }
        }

        public void WriteGraphZUO(ZedGraphControl zgc, List<int> szdz, int ivax, int icount, double lunzhong)
        {
            double imax = 0;
            for (int i = 0; i < icount; i++)
            {

                if (imax < (szdz[i]))
                {
                    imax = (szdz[i]);
                }


            }
            imax = imax * 1.1;

            GraphPane myPane = zgc.GraphPane;
            myPane.Fill = new Fill(Color.White, Color.Aqua, 45.0f);
            myPane.Legend.Fill = new Fill(Color.Gainsboro);
            myPane.Chart.Fill = new Fill(Color.Gainsboro);

            myPane.CurveList.Clear();

            myPane.Title.Text = string.Format("悬架曲线");

            myPane.XAxis.Title.Text = "时间(20ms)";
            myPane.YAxis.Title.Text = "左轮重(kg)";

            myPane.YAxis.MajorGrid.IsVisible = false;
            myPane.YAxis.MinorTic.Size = 0f;

            myPane.YAxis.Scale.Min = 0;
            myPane.YAxis.Scale.Max = imax + 30;


            myPane.YAxis.Scale.MajorStep = (int)(imax + 30) / 10;

            zgc.IsShowPointValues = true;//数据节点
            string ss0 = string.Format("左轮重");
            string ss1 = string.Format("左轮静态");


            PointPairList list0 = new PointPairList();
            PointPairList list1 = new PointPairList();


            LineItem myCurve0 = myPane.AddCurve(ss0, list0, Color.Blue,
                                    SymbolType.None);

            LineItem myCurve1 = myPane.AddCurve(ss1, list1, Color.Red,
                                  SymbolType.None);

            myCurve0.Line.Width = 1.7F;
            myCurve1.Line.Width = 1.7F;


            for (int i = 0; i < icount - 1; i++)
            {

                list0.Add(i, (szdz[i + 1]));
                list1.Add(i, lunzhong);

            }
            zgc.AxisChange();
            zgc.Invalidate();
        }
        public void WriteGrapYOU(ZedGraphControl zgc, List<int> szdz, int ivax, int icount, double lunzhong)
        {
            double imax = 0;
            for (int i = 0; i < icount; i++)
            {

                if (imax < (szdz[i]))
                {
                    imax = (szdz[i]);
                }


            }
            imax = imax * 1.1;


            GraphPane myPane = zgc.GraphPane;
            myPane.Fill = new Fill(Color.White, Color.Aqua, 45.0f);
            myPane.Legend.Fill = new Fill(Color.Gainsboro);
            myPane.Chart.Fill = new Fill(Color.Gainsboro);

            myPane.CurveList.Clear();

            myPane.Title.Text = string.Format("悬架曲线");

            myPane.XAxis.Title.Text = "时间(20ms)";
            myPane.YAxis.Title.Text = "右轮重(kg)";

            myPane.YAxis.MajorGrid.IsVisible = false;
            myPane.YAxis.MinorTic.Size = 0f;

            myPane.YAxis.Scale.Min = 0;
            myPane.YAxis.Scale.Max = imax + 30;


            myPane.YAxis.Scale.MajorStep = (imax + 30) / 10;

            zgc.IsShowPointValues = true;//数据节点
            string ss0 = string.Format("右轮重");
            string ss1 = string.Format("右轮静态");


            PointPairList list0 = new PointPairList();
            PointPairList list1 = new PointPairList();


            LineItem myCurve0 = myPane.AddCurve(ss0, list0, Color.Blue,
                                    SymbolType.None);

            LineItem myCurve1 = myPane.AddCurve(ss1, list1, Color.Red,
                                    SymbolType.None);

            myCurve0.Line.Width = 1.7F;
            myCurve1.Line.Width = 1.7F;


            for (int i = 0; i < icount - 1; i++)
            {

                list0.Add(i, (szdz[i + 1]));
                list1.Add(i, lunzhong);

            }
            zgc.AxisChange();
            zgc.Invalidate();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
           ShowControls(true);
        }
        public void ShowControls(bool flag)
        {
               
                lblhongwai1.Visible = flag;
                label1.Visible = flag;
                label2.Visible = flag;
                label3.Visible = flag;
                label4.Visible = flag;
                gauge_weight_left.Visible = flag;
                gauge_weight_right.Visible = flag;
                gauge_zhidonglizuo.Visible = flag;
                gauge_zhidongliyou.Visible = flag;
                zg2.Visible = flag;
                zedGraphControl1.Visible = flag;
                groupBox3.Visible = flag;

        }
        public void ShowButtons(bool flag)
        {
            button1.Visible = flag;
            button2.Visible = flag;
            button3.Visible = flag;
            button4.Visible = flag;
            but_tiaoling.Visible = flag;
        }

        private void 设备检定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xinxixianshi.Text = "正在检定";
            cbjd = true;
        }

        private void 取消检定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xinxixianshi.Text = "悬架检测";
            cbjd = false;
            flowbass.LedWrite(flowbass.sgongwei, "", 1);
        }

        private void but_tiaoling_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.tiaoling(1);
        }

        private void gauge_zhidongliyou_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            start();//("1", "1");
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.LiftOne(true, (short)(iSUDJz));
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.LiftOne(true, (short)(iSUDJy));
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.LiftOne(false, (short)(iSUDJz));
            m_mainform.DAC.LiftOne(false, (short)(iSUDJy));
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.tiaoling(1);
        }

        private void TESTSU_Shown(object sender, EventArgs e)
        {
            try
            {
                float newx = (this.Width) / x;
                float newy = (this.Height) / y;
                setControls(newx, newy, this);
                ShowControls(false);
                ShowButtons(false);
                this.BackgroundImage = Image.FromFile(@"./ico/悬架背景.jpg");
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            }
            catch (Exception ex)
            {

                m_mainform.baocunriji("悬架Load失败", ex.Message.ToString());//problem
                m_mainform.SaveErrorLog("悬架Load失败", ex.Message.ToString());//problem

            }
        }
    }
}
